package com.jule.teenpatti.game.config;

import com.jule.core.configuration.ConfigurableProcessor;
import com.jule.core.configuration.Property;
import com.jule.core.configuration.ThreadConfig;
import com.jule.core.database.DatabaseConfig;
import com.jule.core.jedis.JedisConfig;
import com.jule.core.utils.PropertiesUtils;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.Properties;

/**
 * @author xujian 2017-12-14
 */
@Slf4j
public class Config {

    @Property(key = "blind.show.roundcount", defaultValue = "1")
    public static int BLIND_SHOW_ROUNDCOUNT;

    @Property(key = "game.bind.ip", defaultValue = "127.0.0.1")
    public static String BIND_IP;

    @Property(key = "game.bind.port", defaultValue = "8080")
    public static int BIND_PORT;

    @Property(key = "notice.forward.port", defaultValue = "localhost:8501")
    public static InetSocketAddress NOTICE_FORWARD_PORT;

    @Property(key = "game.playerLimit", defaultValue = "3000")
    public static int PLAYER_LIMIT;

    @Property(key = "game.ids", defaultValue = "10001")
    public static String GAME_IDS;

    @Property(key = "restful.ip", defaultValue = "127.0.0.1")
    public static  String REST_IP ;

    @Property(key = "restful.port", defaultValue = "8082")
    public static  int REST_PORT ;

    /**日志服*/
    @Property(key = "app.log.url", defaultValue = "http://10.0.0.92:59001")
    public static String LOG_URL;

    /**选择玩法倒计时*/
    @Property(key = "choose.playtype.cd", defaultValue = "10")
    public static int CHOOSE_PLAYTYPE_CD;

    @Property(key = "app.mode.test", defaultValue = "true")
    public static boolean MODE_TEST;
    @Property(key = "load.db.config.hour", defaultValue = "5")
    public static int loadDBConfigHour;

    @Property(key = "game.alarm", defaultValue = "false")
    public static boolean GAME_ALARM;

    @Property(key = "item.server.url", defaultValue = "http://10.0.0.93:8888")
    public static String ITEM_SERVER_URL;

    @Property(key = "game.id", defaultValue = "1001")
    public static int GAME_ID;

    @Property(key = "app.pay.channel", defaultValue = "googleplay")
    public static String DEFUALT_PAY_CHANNEL;

    @Property(key = "item.server.can_unlock_item", defaultValue = "false")
    public static boolean CAN_UNLOCK_ITEM;

    @Property(key = "game.maxBetRound", defaultValue = "10")
    public static int GAME_MAXBETROUND;

    /**
     * Load configs from files.
     */
    public static void load(String dir) {
        try {

            Properties myProps = null;
            try {
                log.info("Loading: mycs.properties");
                myProps = PropertiesUtils.load("./config/mycs.properties");
            } catch (Exception e) {
                log.info("No override properties found");
            }

            Properties[] props = PropertiesUtils.loadAllFromDirectory("./config/network");
            PropertiesUtils.overrideProperties(props, myProps);

            log.info("Loading: game.properties");
            ConfigurableProcessor.process(Config.class, PropertiesUtils.loadAllFromDirectory("./config/network/" + dir + "/"));
            log.info("Loading: redis.properties");
            ConfigurableProcessor.process(JedisConfig.class, props);
            log.info("Loading: database.properties");
            ConfigurableProcessor.process(DatabaseConfig.class, props);
            log.info("Loading: signreach.properties");
//            ConfigurableProcessor.process(SignreachConfig.class, props);
            log.info("Loading: threadpool.properties");
            ConfigurableProcessor.process(ThreadConfig.class, props);
        } catch (Exception e) {
            log.error("Can't load Game configuration", e);
            throw new Error("Can't load Game configuration", e);
        }
    }
}
